package students.StudentInfo;

import javax.swing.*;
import java.awt.*;//导入布局包
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.sql.*;//导入数据库驱动包

public class QueryStudentFrame extends JFrame{
    JPanel buttonPanel = new JPanel();
    JPanel inputPanel = new JPanel(new GridBagLayout());

    //删除按钮
    JButton jb1 = new JButton("查询");
    //取消按钮
    JButton jb2 = new JButton("取消");

    //学号输入框
    JLabel jl1 = new JLabel("学号：");
    JTextField jtf1 = new JTextField();

    public QueryStudentFrame() {
        setTitle("查询学生信息");
        setSize(300, 200);
        setVisible(true);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        //学号输入框
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.anchor = GridBagConstraints.CENTER;

        // 学号标签
        jl1.setPreferredSize(new Dimension(80, 20));
        gbc.gridx = 0;
        gbc.gridy = 0;
        inputPanel.add(jl1, gbc);

        // 输入框
        gbc.gridx = 1;
        jtf1.setPreferredSize(new Dimension(120, 25));
        inputPanel.add(jtf1, gbc);

        // 将输入面板居中
        this.add(inputPanel, BorderLayout.CENTER);

        //添加查询按钮事件
        jb1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取输入的学号
                String sno = jtf1.getText();
                //判断学号是否为空
                if(sno.isEmpty()) {
                    JOptionPane.showMessageDialog(QueryStudentFrame.this, "请输入学号", "提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                //查询学生信息
                try {
                    //创建连接
                    Connection conn = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/project001?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC",
                        "root",
                        "123456"
                    );
                    //创建SQL语句
                    String sql = "select * from student where sno = ?";
                    //创建PreparedStatement对象
                    PreparedStatement pstmt = conn.prepareStatement(sql);
                    //设置参数
                    pstmt.setInt(1, Integer.parseInt(sno));
                    //执行SQL语句
                    try (ResultSet rs = pstmt.executeQuery()) {
                        if (rs.next()) {
                            // 创建信息展示面板
                            JPanel infoPanel = new JPanel(new GridBagLayout());
                            GridBagConstraints gbcInfo = new GridBagConstraints();
                            gbcInfo.insets = new Insets(5, 10, 5, 10);
                            gbcInfo.anchor = GridBagConstraints.WEST;
                            
                            // 学生信息字段
                            addFormRow(infoPanel, gbcInfo, 0, "学号：", rs.getString("sno"));
                            addFormRow(infoPanel, gbcInfo, 1, "姓名：", rs.getString("name"));
                            addFormRow(infoPanel, gbcInfo, 2, "性别：", rs.getString("gender"));
                            addFormRow(infoPanel, gbcInfo, 3, "年龄：", rs.getString("age"));
                            addFormRow(infoPanel, gbcInfo, 4, "班级：", rs.getString("classNo"));
                            addFormRow(infoPanel, gbcInfo, 5, "入学年份：", rs.getString("entryYear"));
                            addFormRow(infoPanel, gbcInfo, 6, "联系电话：", rs.getString("phone"));
                            
                            // 显示结果窗口
                            JDialog resultDialog = new JDialog(QueryStudentFrame.this, "学生详细信息", true);
                            resultDialog.setSize(300, 250);
                            resultDialog.add(new JScrollPane(infoPanel));
                            resultDialog.setLocationRelativeTo(QueryStudentFrame.this);
                            resultDialog.setVisible(true);
                        } else {
                            JOptionPane.showMessageDialog(QueryStudentFrame.this, "未查询到任何记录", "提示", JOptionPane.WARNING_MESSAGE);
                        }
                    }
                    try {
                        int affectedRows = pstmt.executeUpdate();
                        if(affectedRows > 0) {
                            JOptionPane.showMessageDialog(
                                QueryStudentFrame.this, 
                                "成功查询" + affectedRows + "条记录",
                                "操作结果",
                                JOptionPane.INFORMATION_MESSAGE
                            );
                        } else {
                            JOptionPane.showMessageDialog(QueryStudentFrame.this, "未查询到任何记录", "提示", JOptionPane.WARNING_MESSAGE);
                        }   
                    } catch (SQLException ex) {
                        JOptionPane.showMessageDialog(QueryStudentFrame.this, "数据库异常：" + ex.getMessage(), 
                            "严重错误", JOptionPane.ERROR_MESSAGE);
                        ex.printStackTrace();
                    }
                    //关闭连接
                    pstmt.close();
                    conn.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
        //添加取消按钮事件
        jb2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //关闭窗口
                dispose();
            }
        });
        //设置按钮大小
        jb1.setPreferredSize(new Dimension(100, 30));
        jb2.setPreferredSize(new Dimension(100, 30));
        buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
        buttonPanel.add(Box.createHorizontalGlue());
        buttonPanel.add(jb1);
        buttonPanel.add(Box.createRigidArea(new Dimension(20,0)));
        buttonPanel.add(jb2);
        buttonPanel.add(Box.createHorizontalGlue());
        getContentPane().add(buttonPanel, BorderLayout.SOUTH);
    }
    // 添加表单行工具方法
    private void addFormRow(JPanel panel, GridBagConstraints gbc, int row, String label, String value) {
        gbc.gridy = row;
        gbc.gridx = 0;
        panel.add(new JLabel(label), gbc);
        gbc.gridx = 1;
        panel.add(new JLabel(value), gbc);
    }
}
